import tkinter as tk
from tkinter import font
import math
import time

class HeartPopup:
    def __init__(self, message="Love You", duration=5000):
        self.root = tk.Tk()
        self.message = message
        self.duration = duration
        
        # 设置窗口属性
        self.root.overrideredirect(True)  # 无边框
        self.root.attributes('-topmost', True)  # 置顶
        self.root.attributes('-alpha', 0.0)  # 初始透明
        
        # 设置窗口大小和位置
        self.window_width = 400
        self.window_height = 400
        screen_width = self.root.winfo_screenwidth()
        screen_height = self.root.winfo_screenheight()
        x = (screen_width - self.window_width) // 2
        y = (screen_height - self.window_height) // 2
        self.root.geometry(f'{self.window_width}x{self.window_height}+{x}+{y}')
        
        # 创建画布
        self.canvas = tk.Canvas(
            self.root,
            width=self.window_width,
            height=self.window_height,
            highlightthickness=0,
            bg='SystemButtonFace'
        )
        self.canvas.pack()
        
        # 绘制爱心和文字
        self.draw_heart()
        self.draw_text()
        
        # 创建关闭按钮
        self.close_button = tk.Button(
            self.root,
            text="×",
            command=self.fade_out,
            font=('Arial', 16, 'bold'),
            bg='#ff4444',
            fg='white',
            bd=0,
            padx=10,
            pady=5
        )
        self.close_button.place(x=360, y=10)
        
        # 淡入效果
        self.fade_in()
        
        # 设置自动关闭
        if duration > 0:
            self.root.after(duration, self.fade_out)
    
    def draw_heart(self):
        """绘制爱心形状"""
        center_x = self.window_width // 2
        center_y = self.window_height // 2 - 30
        
        # 爱心参数
        size = 80
        points = []
        
        # 使用参数方程绘制爱心
        for angle in range(0, 360, 2):
            t = math.radians(angle)
            x = 16 * math.sin(t) ** 3
            y = -(13 * math.cos(t) - 5 * math.cos(2*t) - 2 * math.cos(3*t) - math.cos(4*t))
            
            # 缩放并移动到中心位置
            x = center_x + x * size / 16
            y = center_y + y * size / 16
            
            points.extend([x, y])
        
        # 绘制爱心
        self.canvas.create_polygon(
            points,
            fill='#ff4444',
            outline='#ff6666',
            width=2
        )
        
        # 添加光泽效果
        self.canvas.create_oval(
            center_x - 30, center_y - 20,
            center_x - 10, center_y,
            fill='#ff6666',
            outline=''
        )
    
    def draw_text(self):
        """绘制文字"""
        center_x = self.window_width // 2
        center_y = self.window_height // 2 + 80
        
        # 创建自定义字体
        custom_font = font.Font(family='Arial', size=24, weight='bold')
        
        # 绘制文字阴影
        self.canvas.create_text(
            center_x + 2, center_y + 2,
            text=self.message,
            font=custom_font,
            fill='#cccccc'
        )
        
        # 绘制主文字
        self.canvas.create_text(
            center_x, center_y,
            text=self.message,
            font=custom_font,
            fill='#ff4444'
        )
    
    def fade_in(self):
        """淡入效果"""
        alpha = self.root.attributes('-alpha')
        if alpha < 1.0:
            alpha = min(alpha + 0.05, 1.0)
            self.root.attributes('-alpha', alpha)
            self.root.after(20, self.fade_in)
    
    def fade_out(self):
        """淡出效果"""
        alpha = self.root.attributes('-alpha')
        if alpha > 0.0:
            alpha = max(alpha - 0.05, 0.0)
            self.root.attributes('-alpha', alpha)
            self.root.after(20, self.fade_out)
        else:
            self.root.destroy()
    
    def run(self):
        """运行弹窗"""
        self.root.mainloop()

def show_heart_popup(message="Love You", duration=5000):
    """显示爱心弹窗的便捷函数"""
    popup = HeartPopup(message, duration)
    popup.run()

if __name__ == "__main__":
    # 示例使用
    show_heart_popup("Love You", 5000)
